﻿/// <reference path="DocumentResourceType.ts" />

module HeadTS {

    export class DocumentResource {
        /// <summary>
        /// A resource that can be added to a document.
        /// </summary>

        constructor(public type: DocumentResourceType, public sources: string[], public name: string = null, public namedDependencies: string[]= [], public test: Predicate = () => true) {
            // If we have no sources defined, throw an exception since we can't do anything without at least one.
            if ((sources === null) || (sources.length === 0)) {
                throw "At least one source is required."
            }

            // If no name was provided, generate one from the first entry.
            if ((name === null) || (name.length === 0)) {
                this.name = ResourceUtilities.createResourceNameFromLocation(sources[0]);
            }
        }

        static createForScript(sources: string[], name?: string, namedDependencies?: string[], test?: Predicate): DocumentResource {
            return new DocumentResource(DocumentResourceType.script, sources, name, namedDependencies, test);
        }

        static createForStyle(sources: string[], name?: string, namedDependencies?: string[], test?: Predicate): DocumentResource {
            return new DocumentResource(DocumentResourceType.style, sources, name, namedDependencies, test);
        }

    }

    class ResourceUtilities {

        static createResourceNameFromLocation(location: string): string {
            var pathComponents = location.split('/');
            return pathComponents[pathComponents.length - 1];
        }
    }

}